package com.appcenter.application.service;


import com.appcenter.application.input.Dic.*;
import com.appcenter.application.mapper.DicsMapper;
import com.appcenter.application.model.Apps;
import com.appcenter.application.model.Dics;
import com.appcenter.application.output.Dic.*;
import com.appcenter.jz.Unit.StringUnit;
import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.page.PagedResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class DicService {
    @Autowired
    DicsMapper dicsMapper;

    public CreateDicOutput CreateDic(CreateDicInput input) {
        CreateDicOutput output = new CreateDicOutput();

        int PrmCount = dicsMapper.getPrmCount(input.getItem().getAppId(), input.getItem().getDicKey()).size();
        if(PrmCount>0){
            throw new ErrorJsonResult("1000041", "此应用下字典主键已存在！");
        }

        int CodeCount = dicsMapper.getCodeCount(input.getItem().getAppId(), input.getItem().getDicKey(), input.getItem().getParentId()).size();
        if (CodeCount > 0) {
            throw new ErrorJsonResult("1000042", "此字典键名下字典编码已存在！");
        }

        int ValCount = dicsMapper.getValCount(input.getItem().getAppId(), input.getItem().getDicKey(), input.getItem().getParentId()).size();
        if (ValCount > 0) {
            throw new ErrorJsonResult("1000043", "此字典键名下字典值已存在！");
        }
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        Dics dic = new Dics();
        dic.setId(uuid);
        dic.setAppId(input.getItem().getAppId());
        dic.setDicCode(input.getItem().getDicCode());
        dic.setDicName(input.getItem().getDicName());
        dic.setDescription(input.getItem().getDescription());
        dic.setParentId(input.getItem().getParentId());

        if (input.getItem().getParentId() != null && input.getItem().getParentId()!="") {
            Dics ParentDic = dicsMapper.selectByPrimaryKey(input.getItem().getParentId());

            if(ParentDic!=null){
                dic.setLevel(ParentDic.getLevel() + 1);
                dic.setDicKey(ParentDic.getDicKey() + "_" + input.getItem().getDicCode());
                dic.setAppId(ParentDic.getAppId());
            }else{
                dic.setLevel(1);
                dic.setDicKey(input.getItem().getDicCode());
                dic.setFullListOrder("");
                dic.setParentId("");
            }
        } else {
            dic.setLevel(1);
            dic.setDicKey(input.getItem().getDicCode());
            dic.setFullListOrder("");
            dic.setParentId("");
        }

        if (input.getItem().getListOrder() == 0) {
            Dics dicMax=dicsMapper.maxListOrderDic(input.getItem().getAppId(),input.getItem().getParentId());
            dic.setListOrder(dicMax==null?0:dicMax.getListOrder()+10);
        }
        String FullListOrder= StringUnit.padLeft(String.valueOf(dic.getListOrder()),5,'0');
        dic.setFullListOrder(dic.getFullListOrder()+FullListOrder);
        dicsMapper.insertSelective(dic);

        output.setItem(dic);
        return output;
    }

    public DeleteDicOutput DeleteDic(DeleteDicInput input) {
        DeleteDicOutput output = new DeleteDicOutput();
        dicsMapper.deleteByPrimaryKey(input.getItem().getId());
        return output;

    }

    public EditDicOutput EditDic(EditDicInput input) {
        EditDicOutput output = new EditDicOutput();

        int PrmCount = dicsMapper.getPrmCountNoId(input.getItem().getAppId(), input.getItem().getDicKey(),input.getItem().getId()).size();
        if(PrmCount>0){
            throw new ErrorJsonResult("1000041", "此应用下字典主键已存在！");
        }

        int CodeCount = dicsMapper.getCodeCountNoId(input.getItem().getAppId(), input.getItem().getDicKey(), input.getItem().getParentId(),input.getItem().getId()).size();
        if (CodeCount > 0) {
            throw new ErrorJsonResult("1000042", "此字典键名下字典编码已存在！");
        }

        int ValCount = dicsMapper.getValCountNoId(input.getItem().getAppId(), input.getItem().getDicKey(), input.getItem().getParentId(),input.getItem().getId()).size();
        if (ValCount > 0) {
            throw new ErrorJsonResult("1000043", "此字典键名下字典值已存在！");
        }
        Dics dic = dicsMapper.selectByPrimaryKey(input.getItem().getId());
        dic.setAppId(input.getItem().getAppId());
        dic.setDicCode(input.getItem().getDicCode());
        dic.setDicName(input.getItem().getDicName());
        dic.setDescription(input.getItem().getDescription());
        dic.setParentId(input.getItem().getParentId());

        if (input.getItem().getParentId() != null && input.getItem().getParentId()!="") {
            Dics ParentDic = dicsMapper.selectByPrimaryKey(input.getItem().getParentId());
            if(ParentDic!=null){
                dic.setLevel(ParentDic.getLevel() + 1);
                dic.setDicKey(ParentDic.getDicKey() + "_" + input.getItem().getDicCode());
                dic.setAppId(ParentDic.getAppId());
            }else{
                dic.setLevel(1);
                dic.setDicKey(input.getItem().getDicCode());
                dic.setFullListOrder("");
                dic.setParentId("");
            }

        } else {
            dic.setLevel(1);
            dic.setDicKey(input.getItem().getDicCode());
            dic.setFullListOrder("");
            dic.setParentId("");
        }

        if (input.getItem().getListOrder() == 0) {
            Dics dicMax=dicsMapper.maxListOrderDic(input.getItem().getAppId(),input.getItem().getParentId());
            dic.setListOrder(dicMax==null?0:dicMax.getListOrder()+10);
        }
        String FullListOrder= StringUnit.padLeft(String.valueOf(dic.getListOrder()),5,'0');
        dic.setFullListOrder(dic.getFullListOrder()+FullListOrder);
        dicsMapper.updateByPrimaryKey(dic);
        output.setItem(dic);

        return output;
    }

    public GetDicsOutput GetDics(GetDicsInput input) {
        GetDicsOutput output = new GetDicsOutput();
        PageHelper.startPage(input.getPage().getPageNo(),input.getPage().getPageSize(),"ListOrder desc");
        List<Dics> ls = dicsMapper.searchDicList(input);
        PageInfo<Dics> page = new PageInfo<>(ls);
        output.setItems(new PagedResult<Dics>(page.getList(),(int)page.getTotal()));
        return output;
    }

    public PageLoadOutput PageLoad(PageLoadInput input) {

        PageLoadOutput output = new PageLoadOutput();
        output.setItems(GetDics(input).getItems());
        return output;
    }

    //---------DicService-----------
    public GetDicsByKeysOutput GetDicsByKeys(GetDicsByKeysInput input) {
        GetDicsByKeysOutput output = new GetDicsByKeysOutput();

       List<Dics> ls=new ArrayList<Dics>();
       for (String dicKey:input.getDicKeys()) {
          Dics dic=dicsMapper.getDicsByKey(input.getAppCode(),dicKey);
          if (dic!=null)
          {
              dic.setChildren(dicsMapper.getChildrenByParentId(dic.getId()));
              ls.add(dic);
          }
       }
        output.setItems(new PagedResult<Dics>(ls,ls.size()));
        return output;
    }
}
